package com.demo.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.demo.cms.dao.ArticleContentDao;
import com.demo.cms.model.ArticleContent;
import com.demo.cms.service.ArticleContentService;
import com.demo.cms.service.exception.ArticleContentCreateException;

/**
 * 文章内容服务实现类
 * 
 * @author ryuu.kk
 *
 */
public class ArticleContentServiceImpl implements ArticleContentService {

	/**
	 * 文章内容数据访问Dao
	 */
	private ArticleContentDao articleContentDao;
	
	@Override
	public Long add(ArticleContent content) throws ArticleContentCreateException {
		Long id = null;
		try {
			id = articleContentDao.insert(content);
		} catch (Exception e) {
			throw new ArticleContentCreateException(content, e.getMessage(), e.getCause());
		}
		return id;
	}

	@Override
	@Transactional
	public void addAll(ArticleContent... contents) throws ArticleContentCreateException {
		if (contents != null && contents.length > 0) {
			int index = 0;
			for (ArticleContent content : contents) {
				//第一条的index为1
				content.setIndex(index + 1);
				this.add(content);
				index ++;
			}
		}
	}

	@Override
	public List<ArticleContent> findList(Long articleId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("articleId", articleId);
		return articleContentDao.select(params);
	}

	@Override
	public ArticleContent find(Long articleId, int index) {
		Map<String, Object> params = new HashMap<String, Object>(5);
		params.put("articleId", articleId);
		params.put("index", index);
		List<ArticleContent> contentList = articleContentDao.select(params);
		return (contentList != null && contentList.size() > 0) ? contentList.get(0) : null;
	}

	@Override
	public void update(ArticleContent content, Long id) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("id", id);
		articleContentDao.update(content, params);
	}

	@Override
	public void update(ArticleContent content, Long articleId, int index) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("articleId", articleId);
		params.put("index", index);
		content.setIndex(index);
		//content.setArticleId(articleId);
		articleContentDao.update(content, params);
	}

	@Override
	@Transactional
	public void update(List<ArticleContent> contents, Long articleId) {
		if (contents != null && contents.size() > 0) {
			for (int index = 0; index < contents.size(); index++) {
				this.update(contents.get(index), articleId, index + 1);
			}
		}
	}

	@Override
	public void deleteAll(Long articleId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("articleId", articleId);
		articleContentDao.delete(params);
	}

	@Override
	public void delete(Long id) {
		articleContentDao.delete(id);
	}
	
	@Override
	public void delete(Long articleId, int index) {
		Map<String, Object> params = new HashMap<String, Object>(5);
		params.put("articleId", articleId);
		params.put("index", index);
		articleContentDao.delete(params);
	}

	public void setArticleContentDao(ArticleContentDao articleContentDao) {
		this.articleContentDao = articleContentDao;
	}
}
